package cfca.paperless.client.servlet.performance;

import java.io.File;
import java.util.Date;

import org.apache.commons.io.FileUtils;

import cfca.paperless.client.bean.SignLocation;
import cfca.paperless.client.servlet.PaperlessClient;
import cfca.paperless.client.servlet.PaperlessClientLocal;
import cfca.paperless.client.servlet.PaperlessLocalConfig;
import cfca.paperless.client.util.GUID;
import cfca.paperless.client.util.IoUtil;
import cfca.paperless.client.util.PaperlessUtil;

public class testSealAutoPdfLocalPerformance {

    public static void main(String[] args) throws Exception {
        try {
            byte[] pdf = FileUtils.readFileToByteArray(new File("./TestData/file.pdf"));

            // 初始化客户端对象
            PaperlessClient clientExtBean = new PaperlessClient(PaperlessLocalConfig.extUrl, 10000, 60000);// 无纸化辅助接口的访问URL

            // 以下代码为获取印章编码对应公钥证书
            PaperlessClient clientAssistBean = new PaperlessClient(PaperlessLocalConfig.assistUrl, 10000, 60000);// 无纸化辅助接口的访问URL

            PaperlessClientLocal paperlessClientLocal = new PaperlessClientLocal(clientExtBean, clientAssistBean, PaperlessLocalConfig.operatorCode);
//            paperlessClientLocal.initTimestampServer("http://127.0.0.1:8080/Proxy/Timestamp", "", "");
            
            long startTime = System.currentTimeMillis();
            for (int i = 0; i < 10; i++) {
                new SealLocalPerformance(pdf, paperlessClientLocal, startTime).start();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

class SealLocalPerformance extends Thread {
    static int successCount;
    static int failueCount;
    static long startTime;

    private synchronized static void successAdd() {
        successCount++;
    }

    private synchronized static void failueAdd() {
        failueCount++;
    }

    PaperlessClientLocal paperlessClientLocal;
    byte[] pdf;

    public SealLocalPerformance(byte[] pdf, PaperlessClientLocal paperlessClientLocal, long startTime) {
        super();
        this.pdf = pdf;
        this.paperlessClientLocal = paperlessClientLocal;
        this.startTime = startTime;
    }

    @Override
    public void run() {
        long timeStart = System.currentTimeMillis();
        boolean f = process(pdf, paperlessClientLocal);
        System.out.println("time:" + ":" + (System.currentTimeMillis() - timeStart));
    }

    private boolean process(byte[] pdf, PaperlessClientLocal paperlessClientLocal) {
        try {
            for (int i = 0; i < 100; i++) {
                SignLocation signLocation = new SignLocation("代理人签名", "R", 0, 0);// 签名位置
                signLocation = new SignLocation(1, 100, 100);
                byte[] sealedPdf = paperlessClientLocal.sealAutoPdfLocal(pdf, "org001", "11111111", "","", signLocation);
                String errorRsString = PaperlessUtil.isError(sealedPdf);
                if ("".equals(errorRsString)) {//System.out.println(".....................................................");
                    SealLocalPerformance.successAdd();
                    String savedPdfId = GUID.generateId();
                    IoUtil.write("./TestData/performance/" + savedPdfId + ".pdf", sealedPdf);
                } else {
                    System.out.println("error:" + errorRsString);
                    SealLocalPerformance.failueAdd();
                }
                if(SealLocalPerformance.successCount % 100 == 0) {
                    long period = (System.currentTimeMillis() - startTime)/1000;
                    long tps = SealLocalPerformance.successCount / period;
                    System.out.println("..................................................................tps:" + tps + ",period:" + period);
                }
//                Thread.sleep(100);
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    }

}
